package com.zeyi.chat.controller;

import com.alibaba.fastjson.JSON;
import com.zeyi.chat.dto.Message;
import com.zeyi.chat.manager.StageManager;
import com.zeyi.chat.utils.AvatarUtil;
import com.zeyi.chat.utils.EmailUtil;
import com.zeyi.chat.utils.FXMLUtil;
import com.zeyi.chat.utils.SocketUtil;
import com.zeyi.chat.views.CircleBorder;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zeyi
 */
public class RegisterController {

    @FXML
    private ImageView avatar;

    @FXML
    public Button codeBtn;

    @FXML
    private Label codeTip;

    @FXML
    private Label emailTip;

    @FXML
    private TextField email;

    @FXML
    private TextField code;

    @FXML
    private Label usernameTip;

    @FXML
    private Label passwordOneTip;

    @FXML
    private Label passwordTwoTip;

    @FXML
    private TextField username;

    @FXML
    private PasswordField passwordOne;

    @FXML
    private PasswordField passwordTwo;

    @FXML
    private Button registerBtn;

    private String avatarPath = "/images/user/default.jpg";
    private File tmpFile;

    @FXML
    private void initialize() {
        Rectangle rectangle = new Rectangle(150, 150);
        rectangle.setArcHeight(150);
        rectangle.setArcWidth(150);
        avatar.setClip(rectangle);
    }

    @FXML
    void backLogin(ActionEvent event) {
        Stage stage = (Stage) registerBtn.getScene().getWindow();
        Scene scene = FXMLUtil.createScene("/Fxml/Login.fxml");
        new CircleBorder(scene, 5, "#DCDFE6");
        new WindowMove(stage, scene.getRoot());
        stage.setScene(scene);
    }

    @FXML
    public void minimize(MouseEvent mouseEvent) {
        Stage stage = (Stage) registerBtn.getScene().getWindow();
        stage.setIconified(true);
    }

    @FXML
    public void onClose(MouseEvent mouseEvent) {
        System.exit(0);
    }

    @FXML
    public void register(ActionEvent actionEvent) {
        if (username.getText().equals("")) {
            usernameTip.setText("请输入用户名");
            return;
        }
        usernameTip.setText("");
        if (passwordOne.getText().equals("")) {
            passwordOneTip.setText("请输入密码");
            return;
        }
        passwordOneTip.setText("");
        if (passwordTwo.getText().equals("")) {
            passwordTwoTip.setText("请输确认密码");
            return;
        }
        if (!passwordOne.getText().equals(passwordTwo.getText())) {
            passwordTwoTip.setText("两次密码不同");
            return;
        }
        passwordTwoTip.setText("");
        if (email.getText().equals("")) {
            emailTip.setText("请输入邮箱");
            return;
        }
        emailTip.setText("");
        if (code.getText().equals("")) {
            codeTip.setText("请输入验证码");
            return;
        }
        codeTip.setText("");
        if (tmpFile != null) {
            avatarPath = AvatarUtil.copyAvatar(tmpFile, "user");
        }
        Map<String, String> map = new HashMap<>();
        map.put("username", username.getText());
        map.put("password", passwordOne.getText());
        map.put("email", email.getText());
        map.put("checkCode", code.getText());
        map.put("avatar", avatarPath);
        SocketUtil.send(new Message("UserHandler/register", "UserHandler/register", JSON.toJSONString(map)));
    }

    @FXML
    public void acquireCode(ActionEvent actionEvent) {
        if (email.getText().equals("")) {
            emailTip.setText("请输入邮箱");
        } else if (!EmailUtil.checkEmail(email.getText())) {
            emailTip.setText("请输入合法的邮箱");
        } else {
            emailTip.setText("");
            codeBtn.setDisable(true);
            int time = 60;
            codeBtn.setText(time + "秒后再获取");
            new Thread(() -> {
                for (int i = 1; i <= time; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    int finalI = i;
                    Platform.runLater(() -> {
                        codeBtn.setText((time - finalI) + "秒后再获取");
                    });
                }
                Platform.runLater(() -> {
                    codeBtn.setText("获取");
                    codeBtn.setDisable(false);
                });
            }).start();
            SocketUtil.send(new Message("UserHandler/email", "UserHandler/email", email.getText()));
        }
    }

    @FXML
    public void chooseAvatar(ActionEvent actionEvent) {
        tmpFile = AvatarUtil.chooseAvatar(StageManager.getPrimaryStage(), avatar, "user");
    }
}
